package dao;

import java.util.List;
import modelo.Reserva;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import util.HibernateUtil;

public class ReservaDAOHibernate implements ReservaDAO {

    @Override
    public boolean criar(Reserva reserva) {
        boolean flag;
        Session sessao = HibernateUtil.getSessionFactory().getCurrentSession();
        try {
            //sessao.beginTransaction();
            sessao.getTransaction().begin();
            sessao.save(reserva);
            //sessao.beginTransaction().commit();
            sessao.getTransaction().commit();
            flag = true;
        } catch (HibernateException e) {
            System.err.println("Exceção:" + e);
            flag = false;
            //sessao.beginTransaction().rollback();
            sessao.getTransaction().rollback();
        }
        return flag;
    }

    @Override
    public List<Reserva> listar() {
        List<Reserva> lista = null;
        Session sessao = HibernateUtil.getSessionFactory().getCurrentSession();
        String sql = "from Reserva r left join fetch r.recurso";
        try {
            //sessao.beginTransaction();
            sessao.getTransaction().begin();
            lista = sessao.createQuery(sql).list();
            //sessao.beginTransaction().commit();
            sessao.getTransaction().commit();
        } catch (HibernateException e) {
            System.err.println("Exceção:" + e);
            //sessao.beginTransaction().rollback();
            sessao.getTransaction().rollback();
        }
        return lista;
    }

    @Override
    public boolean alterar(Reserva reserva) {
        boolean flag;
        Session sessao = HibernateUtil.getSessionFactory().getCurrentSession();
        try {
            //sessao.beginTransaction();
            sessao.getTransaction().begin();
            Reserva reservaDB = (Reserva) sessao.load(Reserva.class, reserva.getCodReserva());
            reservaDB.setRecurso(reserva.getRecurso());
            reservaDB.setDataReserva(reserva.getDataReserva());
            reservaDB.setHoraInicio(reserva.getHoraInicio());
            reservaDB.setHoraFim(reserva.getHoraFim());
            sessao.update(reservaDB);
            //sessao.beginTransaction().commit();
            sessao.getTransaction().commit();
            flag = true;
        } catch (HibernateException e) {
            System.err.println("Exceção:" + e);
            flag = false;
            //sessao.beginTransaction().rollback();
            sessao.getTransaction().rollback();
        }
        return flag;
    }

    @Override
    public boolean excluir(Reserva reserva) {
        boolean flag;
        Session sessao = HibernateUtil.getSessionFactory().getCurrentSession();
        try {
            //sessao.beginTransaction();
            sessao.getTransaction().begin();
            Reserva reservaDB = (Reserva) sessao.load(Reserva.class, reserva.getCodReserva());
            sessao.delete(reservaDB);
            //sessao.beginTransaction().commit();
            sessao.getTransaction().commit();
            flag = true;
        } catch (HibernateException e) {
            System.err.println("Exceção:" + e);
            flag = false;
            //sessao.beginTransaction().rollback();
            sessao.getTransaction().rollback();
        }
        return flag;
    }

}
